package org.wii.base.bean;

import lombok.Data;
import org.wii.base.exception.IWiiException;
import org.wii.base.exception.constant.IExceptionErrCode;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 返回结果Bean
 * @author tanghong on 2022/12/22
 */
@Data
public class ResultBean<T> implements Serializable {
    /**返回编码*/
    private long code=0;
    /**返回消息描述*/
    private String message;
    /**返回数据*/
    private T data;

    public boolean isSuccess(){
        return this.code == 0;
    }

    public Map<String, Object> toMap(){
        Map<String, Object> result = new HashMap<>();
        result.put("code", this.code);
        result.put("message", this.message);
        result.put("data", this.data);
        return result;
    }

    public static <T> ResultBean<T> success(String message, T data){
        ResultBean<T> resultBean = new ResultBean<>();
        resultBean.setMessage(message);
        resultBean.setData(data);
        return resultBean;
    }

    public static <T> ResultBean<T> success(T data){
        ResultBean<T> resultBean = new ResultBean<>();
        resultBean.setData(data);
        return resultBean;
    }

    public static <T> ResultBean<T> result(long code, String message){
        ResultBean<T> resultBean = new ResultBean<>();
        resultBean.setCode(code);
        resultBean.setMessage(message);
        return resultBean;
    }

    public static <T> ResultBean<T> result(IWiiException exceptionErrCode){
        ResultBean<T> resultBean = new ResultBean<>();
        resultBean.setCode(exceptionErrCode.getCode());
        resultBean.setMessage(exceptionErrCode.getMessage());
        return resultBean;
    }

}
